package com.itcast.activiti.cmd;

import lombok.AllArgsConstructor;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ByteArrayEntity;
import org.activiti.engine.impl.persistence.entity.ResourceEntity;

import java.util.List;

@AllArgsConstructor
public class UpdateByteArrayEntityCmd implements Command<Void> {
    protected String processDefinitionId;
    protected String deploymentId;
    protected byte[] bytes;

    @Override
    public Void execute(CommandContext commandContext) {
        List<ResourceEntity> resourcesByDeploymentId = commandContext.getResourceEntityManager().findResourcesByDeploymentId(deploymentId);
        ResourceEntity resourceEntity = resourcesByDeploymentId.get(0);
        ByteArrayEntity byteArrayEntity = commandContext.getByteArrayEntityManager().findById(resourceEntity.getId());
        byteArrayEntity.setBytes(bytes);
        commandContext.getByteArrayEntityManager().update(byteArrayEntity);
        commandContext.getProcessEngineConfiguration().getDeploymentManager().getProcessDefinitionCache().remove(processDefinitionId);
        return null;
    }
}
